<template>
  <!-- 进行查询的数据字典的列表 -->
  <div>
    <div class="el-toolbar">
      <div class="el-toolbar-body" style="justify-content: flex-start">
        <a
          href="http://localhost:8202/admin/cmn/dict/exportData"
          target="_blank"
        >
          <el-button type="text"><i class="fa fa-plus" /> 导出</el-button>
        </a>
        <el-button type="text" @click="importData"
          ><i class="fa fa-plus" /> 导入</el-button
        >
        <el-button type="text" @click="exportData"><i class="fa fa-plus" /> 导出-new</el-button>
      </div>
    </div>
    <el-table
      :data="list"
      style="width: 100%"
      row-key="id"
      border
      lazy
      :load="getChildrens"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="名称" width="230" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="编码" width="220">
        <template slot-scope="{ row }">
          {{ row.dictCode }}
        </template>
      </el-table-column>

      <el-table-column label="值" width="230" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.value }}</span>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="导入" :visible.sync="dialogImportVisible" width="480px">
      <el-form label-position="right" label-width="170px">
        <el-form-item label="文件">
          <el-upload
            :multiple="false"
            :on-success="onUploadSuccess"
            :action="'http://localhost:8202/admin/cmn/dict/importData'"
            class="upload-demo"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传xls文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogImportVisible = false"> 取消 </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import dict from "@/api/dict"
import axios from 'axios'
export default {
  data() {
    return {
      list: [], //数据字典列表数组
      dialogImportVisible: false, // 进行控制文件上传的弹出层是否出现
    };
  },
  // 初始化页面进行渲染
  created() {
    this.getDictList(1);
  },
  methods: {
    // 导入数据字典
    importData() {
      this.dialogImportVisible = true;
    },
    // 导入数据字典
    exportData() {
      axios({ // 用axios发送post请求
        method: 'get',
        url: 'http://localhost:8202/admin/cmn/dict/export-dict', // 请求地址
        responseType: 'blob', // 表明返回服务器返回的数据类型
        headers: {
      'Content-Type': 'application/json'
      }
    }).then(res => { // 处理返回的文件流
      const blob = new Blob([res.data],{type: "application/vnd.ms-excel"});//new Blob([res])中不加data就会返回下图中[objece objece]内容（少取一层）
      const fileName = '统计.xlsx';//下载文件名称
      const elink = document.createElement('a');
      elink.download = fileName;
      elink.style.display = 'none';
      elink.href = URL.createObjectURL(blob);
      document.body.appendChild(elink);
      elink.click();
      URL.revokeObjectURL(elink.href); // 释放URL 对象
      document.body.removeChild(elink);
    })
      // dict.exportData().then((response)=>{
      //   console.log('12')
      //   this.downloadFile('blob',response,'123')
      // })
    },
    // 上传文件数据成功操作
    onUploadSuccess() {
      // 1.进行关闭弹窗
      this.dialogImportVisible = false;
      // 2.进行刷新数据
      this.getDictList(1);
    },
    // 数据字典列表
    getDictList(id) {
      dict.dictList(id).then((response) => {
        this.list = response.data;
      });
    },
    // 获取子数据
    getChildrens(tree, treeNode, resolve) {
      dict.dictList(tree.id).then((response) => {
        resolve(response.data);
      });
    },
    downloadFile(contentType, data, fileName) {
    const byteCharacters = atob(data);
    const byteNumbers = new Array(byteCharacters.length);
    for (let i = 0; i < byteCharacters.length; i++) {
      byteNumbers[i] = byteCharacters.charCodeAt(i);
    }
    const byteArray = new Uint8Array(byteNumbers);
    const blob = new Blob([byteArray], {
      type: contentType,
    });
    const tempLink = document.createElement('a');
    tempLink.href = window.URL.createObjectURL(blob);
    tempLink.download = fileName;
    tempLink.target = '_blank';
    tempLink.click();
  }
  },
};
</script>